package com.multidisplay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multidisplay.bo.ResultBo;
import com.multidisplay.mapper.BaseDataMapper;
import com.multidisplay.po.BaseData;
import com.multidisplay.po.User;
import com.multidisplay.service.BaseDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("BaseDataService")
public class BaseDataServiceImpl implements BaseDataService {
    @Resource
    private BaseDataMapper baseDataMapper;

    @Override
    public int add(BaseData baseData) {
        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", baseData.getUrl());
        List<BaseData> baseData1 = baseDataMapper.selectList(queryWrapper);
        if(!baseData1.isEmpty()) {
            return -1;
        }
        try {
            baseDataMapper.insert(baseData);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void delById(int id) throws Exception {
        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
            baseDataMapper.delete(queryWrapper);
    }

    @Override
    public List<BaseData> selectById(int id) {
        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<BaseData> baseDatas = baseDataMapper.selectList(queryWrapper);
        return baseDatas;
    }

    @Override
    public List<BaseData> selectByName(String name) {
        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<BaseData> baseDatas = baseDataMapper.selectList(queryWrapper);
        return baseDatas;
    }

    @Override
    public ResultBo<BaseData> selectAll(int page,int limit) {
        IPage iPage = new Page(page,limit);
        IPage<BaseData> baseDataIPage = baseDataMapper.selectPage(iPage,null);
        ResultBo<BaseData> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setMsg("");
        resultBo.setCount(baseDataIPage.getTotal());
        resultBo.setData(baseDataIPage.getRecords());
        return resultBo;
    }

    @Override
    public void update(BaseData baseData) {
        baseDataMapper.update(baseData,null);
    }
}
